<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019/3/25
 * Time: 14:09
 */

namespace app\api\controller;
use think\Db;

class Process extends Main
{
    public $uid;
    public $username;
    public $truename;
    public $password;
    public $company_id;
    public $type;
    public $lastversion;//上传版本号
    public $officeManage = 3;//1总公司总管理员2子公司管理员3员工

    public function _initialize()
    {
        $config = config('database');//数据库配置
        $this->lastversion = $config['apiproductversion'];   //上传版本号
    }

    /**
     * 用户验证
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function verifyLogin()
    {
        $this->username = get('username');
        $this->password = get('password');
        $res = $this->detectLogin($this->username,$this->password);
        if($res['state'] == false){
            $this->returnJson($res['content']);
        }
        $user = $res['content'];
        $this->uid = $user['id'];
        $this->company_id = $user['company_id'];
        $this->type = $user['type'];
        $this->truename = $user['truename'];
        //总公司总管理员
        if (($user['super'] == 2) && ($user['pid'] == 1) && ($user['company_id'] == 0) && ($user['type'] == 1)) {
            $this->officeManage = 1;
        }
        //子管理员
        if (($user['super'] == 3) && ($user['type'] == 2)) {
            $this->officeManage = 2;
        }
    }

    /**
     * 入口
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){
        //用户登录验证
        $this->verifyLogin();

        $data = json_decode(post('data'), true);
        $version = $data['Version'];
        //版本验证
        if ($this->lastversion !== $version) {
            $this->returnJson("UnVersion");
        }

        $action = get('action');
        switch ($action) {
            case 'delfile'://删除文件夹
                $this->delFiles($data);
                break;
        }
    }

    private function delFiles($data = []){
        if(isset($data['ID'])&&!empty($data['ID'])){
            DB::name('del_file')->where('id','in',$data['ID'])->delete();
        }
        $res = DB::name('del_file')
            ->field('id,path')
            ->where('user_id', $this->uid)
            ->limit(10)
            ->order('id asc')
            ->select();
        
        echo json_encode($res);exit;
    }
}